/*
 *
 * 过滤器
 */

import dayjs from 'dayjs';

export default {
  // 手机号的过滤
  phoneFilter: function (val) {
    var _val = val + '';
    return _val.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
  },
  // 视频格式化时长
  MillisecondToDate (msd) {
    let time = parseFloat(msd);
    if (time !== null && time !== '') {
      if (time >= 60 && time < 60 * 60) {
        let minute = parseInt(time / 60.0) + '';
        let second = parseInt((parseFloat(time / 60.0) - parseInt(time / 60.0)) * 60) + '';
        time = minute.padStart(2, '0') + ':' + second.padStart(2, '0');
      } else if (time >= 60 * 60 && time < 60 * 60 * 24) {
        let hour = parseInt(time / 3600.0) + '';
        let minute = parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) + '';
        let second = parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) -
          parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60) + '';
        time = hour.padStart(2, '0') + ':' + minute.padStart(2, '0') + ':' + second.padStart(2, '0');
      } else {
        let temp = parseInt(time) + '';
        time = temp.padStart(5, '00:00');
      }
    } else {
      time = '00:00';
    }
    return time;
  },
  // 格式化规格
  formatSpec (value) {
    var Str = '';
    value.map((item) => {
      Str += item.specItemName + ' ';
    });
    return Str;
  },
  // 格式化时间戳
  formatDate: function (value, formatString) {
    formatString = formatString || 'YYYY-MM-DD HH:mm:ss';
    if ((value + '').length === 10) {
      value = value * 1000;
    }
    return dayjs(value).format(formatString);
  },
  commentTimeHandle (dateStr) {
    var _dateStr = dayjs(dateStr).valueOf();
    var publishTime = _dateStr / 1000;
    var date = new Date(publishTime * 1000);
    var Y = date.getFullYear();
    var M = date.getMonth() + 1;
    var D = date.getDate();
    var H = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    if (M < 10) {
      M = '0' + M;
    }
    if (D < 10) {
      D = '0' + D;
    }
    if (H < 10) {
      H = '0' + H;
    }
    if (m < 10) {
      m = '0' + m;
    }
    if (s < 10) {
      s = '0' + s;
    }
    var nowTime = new Date().getTime() / 1000;
    var diffValue = nowTime - publishTime;
    var diff_days = parseInt(diffValue / 86400);
    var diff_hours = parseInt(diffValue / 3600);
    var diff_minutes = parseInt(diffValue / 60);
    var diff_secodes = parseInt(diffValue);

    if (diff_days > 0 && diff_days < 3) {
      return diff_days + '天前';
    } else if (diff_days <= 0 && diff_hours > 0) {
      return diff_hours + '小时前';
    } else if (diff_hours <= 0 && diff_minutes > 0) {
      return diff_minutes + '分钟前';
    } else if (diff_secodes < 60) {
      if (diff_secodes <= 0) {
        return '刚刚';
      } else {
        return diff_secodes + '秒前';
      }
    } else {
      return Y + '-' + M + '-' + D + ' ' + H + ':' + m;
    }
  },
  // 截断，不忽略末尾的0
  toDecimalByCutoffWithoutOmit: function (value = 0, digits = 2, source) {
    let res = value;
    if (Number(value)) {
      let n;
      if (Number(value) > 0) {
        n = Math.floor(Number(value) * Math.pow(10, digits));
      } else {
        n = Math.ceil(Number(value) * Math.pow(10, digits));
      }
      res = (n / Math.pow(10, digits)).toFixed(digits);
    } else if (Number(value) === 0) {
      res = (0).toFixed(digits);
    }
    return res;
  },
  // 保留2位小数
  toFixed (str) {
    str = str + '';
    var _str = str.replace(/\.{2,}/g, '.').replace(/[^\d.]/g, '');
    if (_str) {
      return _str.match(/^\d+(?:\.\d{0,2})?/)[0];
    } else {
      return _str;
    }
  }
};
